package com.cskaoyan.servlet;

import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.PageParameter;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.service.LogService;
import com.cskaoyan.service.impl.LogServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @Author Pudding
 * @Date 2024/5/29 21:40
 */
@WebServlet("/admin/log/*")
public class AdminLogServlet extends CommonServlet {

    LogService logService = new LogServiceImpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //1. 获取GET请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //1.1 可以将请求参数通过BeanUtils封装到PageParameter这个对象中
        PageParameter pageParameter = new PageParameter();
        BeanUtils.copyProperties(pageParameter, parameterMap);
        //1.2 将多余的请求参数再单独获取一次，这样的好处是，不需要在定义一个bean实体类封装这些数据了
        String name = request.getParameter("name");

        //2. 将请求参数传递给service，通过service处理，来获取对应的响应报文中的data数据
        PageData data = logService.list(pageParameter,name);

        //3. 将响应报文中的data数据封装给BaseRespVo，作为响应报文传递给前端，注意：这里直接调用方法即可
        return BaseRespVo.ok(data);
    }
}
